package com.aitaoxue.palx.fragment;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import butterknife.ButterKnife;
import butterknife.InjectView;

import com.aitaoxue.palx.R;
import com.aitaoxue.palx.api.remote.AitaoXueApi;
import com.aitaoxue.palx.base.BaseFragment;
import com.aitaoxue.palx.db.GoodDBHelper;
import com.aitaoxue.palx.ui.MyCourseActivity;
import com.aitaoxue.palx.ui.register.GuideChooseActivity;
import com.loopj.android.http.AsyncHttpResponseHandler;

public class AddCourseFragment extends BaseFragment {

	private static final int REQUEST_COURSE = 2;

	@InjectView(R.id.layout_2)
	RelativeLayout courseLayout;
	@InjectView(R.id.layout_3)
	RelativeLayout priceLayout;
	@InjectView(R.id.layout_4)
	RelativeLayout minmumLayout;
	@InjectView(R.id.course_content)
	TextView course;
	@InjectView(R.id.exp_content)
	EditText price;
	@InjectView(R.id.address_content)
	EditText minmum;
	@InjectView(R.id.btn_push)
	Button push;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.add_new_course, container, false);

		ButterKnife.inject(this, view);
		initView(view);

		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_2:
			Intent intent2 = new Intent(getActivity(),
					GuideChooseActivity.class);
			intent2.putExtra("choose", "course");
			startActivityForResult(intent2, REQUEST_COURSE);
			break;
		case R.id.btn_push:
			if (checkIfReady()) {
				((MyCourseActivity) getActivity()).showWaitDialog();
				String res = GoodDBHelper.getKindIdFromName(course.getText()
						.toString());

				AitaoXueApi.addCourse(res, price.getText().toString(), minmum
						.getText().toString(), mHandler);
			}
			break;
		default:
			break;
		}
	}

	@Override
	public void initView(View view) {
		courseLayout.setOnClickListener(this);
		push.setOnClickListener(this);
	}

	@Override
	public void initData() {

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == getActivity().RESULT_OK) {
			String res = data.getStringExtra("res");
			switch (requestCode) {
			case REQUEST_COURSE:
				course.setText(res);
				break;
			default:
				break;
			}
		}
	}

	private AsyncHttpResponseHandler mHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
			((MyCourseActivity) getActivity()).hideWaitDialog();
			String res = new String(arg2);

			try {
				JSONObject json = new JSONObject(res);
				String code = json.getString("code");
				if (code.equals("0000")) {
					((MyCourseActivity) getActivity()).showToast("添加成功", 0, 0);
				
					//((MyCourseActivity) getActivity()).changeToMyCourseFragment();
					getActivity().onBackPressed();
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onFailure(int arg0, Header[] arg1, byte[] arg2,
				Throwable arg3) {

		}
	};

	private boolean checkIfReady() {
		if (TextUtils.isEmpty(course.getText().toString())) {
			((MyCourseActivity) getActivity()).showToast("请选择科目", 0, 0);
			return false;
		}
		if (TextUtils.isEmpty(price.getText().toString())
				|| !notANumber(price.getText().toString())) {
			((MyCourseActivity) getActivity()).showToast("请输入正确的价格", 0, 0);
			return false;
		}
		if (TextUtils.isEmpty(minmum.getText().toString())
				|| !notANumber(minmum.getText().toString())) {
			((MyCourseActivity) getActivity()).showToast("请输入正确的最低起订数", 0, 0);
			return false;
		}

		return true;
	}

	private boolean notANumber(String num) {
		try {
			int ans = Integer.parseInt(num);
		} catch (Exception e) {
			return false;
		}

		return true;
	}
	
	

}
